﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.IO;

namespace TextEditor
{
    public partial class frmTextEditor : Form
    {
        public frmTextEditor()
        {
            InitializeComponent();
            Text = windowTitle;
        }

        string LastFileName = "";
        string windowTitle = "Text Editor";

        private void btnLoad_Click(object sender, EventArgs e)
        {
        }

        private void btnSave_Click(object sender, EventArgs e)
        {           
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            open();
        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveAs();
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            save();
        }

        private void saveAs()
        {
            TextWriter tw;
            savTextFile.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
            savTextFile.ShowDialog();
            if (savTextFile.FileName != "")
            {
                LastFileName = savTextFile.FileName.ToString();
                try
                {
                    tw = File.CreateText(savTextFile.FileName);
                    //tw.Write(txtEditor.Text);                         // Is there any reason not to use this method to write the file rather than the following one?
                    foreach (string line in txtEditor.Lines)
                    {
                        tw.WriteLine(line);
                    }
                    tw.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error saving File!");
                }
            }
        }

        private void save()
        {
            TextWriter tw;
            try
            {
                tw = File.CreateText(LastFileName);
                //tw.Write(txtEditor.Text);                         // Is there any reason not to use this method to write the file rather than the following one?
                foreach (string line in txtEditor.Lines)
                {
                    tw.WriteLine(line);
                }
                tw.Close();
            }
            catch (Exception ex)
            {
                saveAs();
            }
        }

        private void open()
        {
            TextReader tr;
            opnTextFile.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
            opnTextFile.ShowDialog();
            if (opnTextFile.FileName != "")
            {
                LastFileName = opnTextFile.FileName.ToString();
                Text = windowTitle + " - " + LastFileName;
                try
                {
                    tr = File.OpenText(opnTextFile.FileName);
                    txtEditor.Text = tr.ReadToEnd();
                    tr.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error opening File!");
                }
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }

        private void wordWrapToolStripMenuItem_Click(object sender, EventArgs e)
        {
            mnuWordWrap.Checked = !mnuWordWrap.Checked;
            txtEditor.WordWrap = mnuWordWrap.Checked;
            txtEditor.Select(0, 0);
        }

        private void mnuNew_Click(object sender, EventArgs e)
        {
            txtEditor.Clear();
            LastFileName = "";
        }
    }
}
